<?php
/*
 * IKPHP 爱客开源社区 @copyright (c) 2012-3000 IKPHP All Rights Reserved 
 * @author 小麦
 * @Email:ikphp@sina.cn 2014年8月27日16:58:00
 * @小组应用
 */
namespace IKPHP\Apps\Group\Controllers;

use IKPHP\Apps\Group\Controllers;
use IKPHP\Common\Models\User;
use IKPHP\Apps\Group\Models\GroupCate;
use IKPHP\Apps\Group\Models\Group;
use IKPHP\Apps\Group\Models\GroupTopics;

class ExploreController extends GroupBaseController
{
	private $userid = 0;
	
    public function initialize()
    {
        parent::initialize();
        // 访问者控制
		if ($this->visitor){
			$this->userid = $this->visitor['userid'];
		}        
    } 
    //发现小组首页
    public function indexAction($page = 1)
    { 
		$cateid = $this->_get('cid','intval,trim','0');
		$strCate='';
		//显示列表
		if($cateid){
			$where = "isshow=0 AND isaudit=0 AND cateid = '$cateid'";
			$count = Group::countGroup($where); 
			$pager = $this->_pager($count, 30, array('cid'=>$cateid));	
			$strCate = GroupCate::getOneCate($cateid); 
		}else{
			$where = "isshow=0 AND isaudit=0";
			$count = Group::countGroup($where); 
			$pager = $this->_pager($count, 30);	
		}
		$limit = array("number"=>$pager->listRows, 'offset'=>$pager->firstRow);

		$arrGroup = Group::getGroupByCateid($where,$limit,$this->userid);
		$this->assign('arrGroup', $arrGroup);
		$this->assign('pageUrl', $pager->show());

		//获取子类
		$arrParentCate = GroupCate::getCates();
		$arrGroupCate  =  array();
		foreach($arrParentCate as $key=>$v){
			$arrGroupCate[$key]['pcate'] = $v; 
			$arrGroupCate[$key]['childCate'] =  GroupCate::getReferCate($v['cateid']);
		}
		$this->assign('arrGroupCate', $arrGroupCate);
    		
		if(empty($strCate)){
			$title = "发现小组";
		}else{
			$title = '发现关于"'.$strCate['catename'].'"的小组';
		}
		$this->_config_seo ( array (
				'title' => $title,
				'subtitle'=> '小组_'.C('ik_site_title'),
				'keywords' => '官方小组,科技,智趣,生活,情感,开源,APP下载,案例展示,二次开发,艺术,闲聊,情趣,兴趣,风格模板,php讨论',
				'description'=> '',
		) ); 
		$this->view->pick('explore/groups');
		
    }
	/**
	 * 获取全站精选的的话题 推荐最多；评论最多；喜欢最多；置顶；精华；浏览多
	 **/
	public function topicsAction($cate='topics'){
		if($cate=='topics'){
			//精选
			$count = GroupTopics::countAllEssence(); 
			$pager = $this->_pager($count, 40);	
			$limit = array("number"=>$pager->listRows, 'offset'=>$pager->firstRow);
			$arrExplore = GroupTopics::getAllEssence($limit);
		
			$this->assign('arrExplore', $arrExplore);
			$this->assign('pageUrl', $pager->show());
	
			//值得加入的小组
			$arrEssence = Group::getEssenceGroup(15, $this->userid);
			$this->assign('arrEssence', $arrEssence);
			
			$this->_config_seo ( array (
					'title' => '话题精选',
					'subtitle'=>'小组_'.C('ik_site_title'),
					'keywords' =>'',
					'description'=>'',
			) );  
									
		}else{
					
			$strCate = GroupCate::getOneCate($cate); 
			if(!$strCate) return $this->_redirect('group/explore/topics');
			$arrCateChild = GroupCate::getReferCate($strCate['cateid']);

			$where = "";
			//子类
			$cid = $this->_get('item','intval');
			if($cid){
				$cateid = $cid;
				$strItemCate = GroupCate::getOneCate($cid); 
				$where = "content LIKE '%".$strItemCate['catename']."%'";
			}else{
				$cateid = $strCate['cateid'];
				foreach ($arrCateChild as $key=>$val){
					if($key==0){
						$where .= "content LIKE '%".$val['catename']."%'";
					}else{
						$where .= " OR content LIKE '%".$val['catename']."%'";
					}
				}
			}
			$where .=" AND isshow=0  AND isaudit=0 AND title<>'0'"; 
			//开始按分类搜索
			$count = GroupTopics::count($where); 
			$pager = $this->_pager($count, 40);	
			$limit = array("number"=>$pager->listRows, 'offset'=>$pager->firstRow);
			$arrExplore = GroupTopics::getTopicsByCateName($where, $limit);
		
			$this->assign('arrExplore', $arrExplore);
			$this->assign('pageUrl', $pager->show());			
			
			
			//值得加入的小组
			$arrEssence = Group::getEssenceGroup(15, $this->userid);
			$this->assign('arrEssence', $arrEssence);
			
			$this->assign('arrCateChild', $arrCateChild);
			$this->assign('cateid',$cateid);
			$this->assign('strCate', $strCate);
			$this->_config_seo ( array (
					'title' => $strCate['catename'],
					'subtitle'=>'小组_'.C('ik_site_title'),
					'keywords' =>'',
					'description'=>'',
			) );

			$this->view->pick('explore/search');
		}
	}
}